<?php

namespace App\Http\Middleware;

use App\Helpers\ApiResponse;
use App\Models\AdminLog;
use App\Services\client\PlatformUserService;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use Symfony\Component\HttpFoundation\Response;

/**
 * 路由中间件 - 平台后台验证登录
 */
class PlatformVerifySession
{
    use ApiResponse;

    /**
     * 处理传入的请求。
     *
     * @param  \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response)  $next
     */
    public function handle(Request $request, Closure $next): Response
    {
        $requestAll = $request->all();
        // 判断打开的是管理端还是客户端
        if ($request->session()->has('loginType')) {
            $loginType = $request->session()->get('loginType'); // 登录类型

            // 验证登陆信息
            if (! $request->session()->has(LOGIN_USER)) {
                if ($loginType == 'platform') {
                    if ($request->ajax()) {
                        return $this->fail(['url' => config('admin.admin.common.PLATFORM_LOGIN_PAGE')], '请先登录');
                    } else {
                        return Redirect::to('/403')->with('data', ['msg' => '请先登录', 'url' => config('admin.admin.common.PLATFORM_LOGIN_PAGE')]);
                    }
                } elseif ($loginType == 'admin') {
                    if ($request->ajax()) {
                        return $this->fail(['url' => config('admin.admin.common.ADMIN_LOGIN_PAGE')], '请先登录');
                    } else {
                        return Redirect::to('/403')->with('data', ['msg' => '请先登录', 'url' => config('admin.admin.common.ADMIN_LOGIN_PAGE')]);
                    }
                }
            }

            // 验证平台信息
            $platform = $request->route('platform');
            if (! $platform) {
                if ($loginType == 'platform') {
                    if ($request->ajax()) {
                        return $this->fail(['url' => config('admin.admin.common.PLATFORM_LOGIN_PAGE')], '未查到到平台信息');
                    } else {
                        return Redirect::to('/403')->with('data', ['msg' => '请先登录', 'url' => config('admin.admin.common.PLATFORM_LOGIN_PAGE')]);
                    }
                } elseif ($loginType == 'admin') {
                    if ($request->ajax()) {
                        return $this->fail(['url' => config('admin.admin.common.ADMIN_LOGIN_PAGE')], '未查到到平台信息');
                    } else {
                        return Redirect::to('/403')->with('data', ['msg' => '未查到到平台信息', 'url' => config('admin.admin.common.ADMIN_LOGIN_PAGE')]);
                    }
                }
            }

            $platformDecrypt = customDecrypt($platform);
            if ($platformDecrypt == false) {
                if ($loginType == 'platform') {
                    if ($request->ajax()) {
                        return $this->fail(['url' => config('admin.admin.common.PLATFORM_LOGIN_PAGE')], '未查到到平台信息');
                    } else {
                        return Redirect::to('/403')->with('data', ['msg' => '请先登录', 'url' => config('admin.admin.common.PLATFORM_LOGIN_PAGE')]);
                    }
                } elseif ($loginType == 'admin') {
                    if ($request->ajax()) {
                        return $this->fail(['url' => config('admin.admin.common.ADMIN_LOGIN_PAGE')], '未查到到平台信息');
                    } else {
                        return Redirect::to('/403')->with('data', ['msg' => '未查到到平台信息', 'url' => config('admin.admin.common.ADMIN_LOGIN_PAGE')]);
                    }
                }
            }

            // 站点配置
            $seeting = baseVar();
            //判断站点是否开放
            if ($seeting['admin']['login']['business_state'] === false) {
                if ($loginType == 'platform') {
                    if ($request->ajax()) {
                        return $this->fail(['url' => config('admin.admin.common.PLATFORM_LOGIN_PAGE')], $seeting['admin']['login']['business_description']);
                    } else {
                        return Redirect::to('/403')->with('data', ['msg' => $seeting['admin']['login']['business_description'], 'url' => config('admin.admin.common.PLATFORM_LOGIN_PAGE')]);
                    }
                } elseif ($loginType == 'admin') {
                    if ($request->ajax()) {
                        return $this->fail(['url' => config('admin.admin.common.ADMIN_LOGIN_PAGE')], $seeting['admin']['login']['business_description']);
                    } else {
                        return Redirect::to('/403')->with('data', ['msg' => $seeting['admin']['login']['business_description'], 'url' => config('admin.admin.common.ADMIN_LOGIN_PAGE')]);
                    }
                }
            }

            // 登录信息
            $loginInfo = json_decode($request->session()->get(LOGIN_USER), true);
            // 平台用户信息
            if ($loginType == 'platform') {
                $puser = PlatformUserService::infoByUserid($platformDecrypt['userid'], $platformDecrypt['platform']->id);
            } elseif ($loginType == 'admin') {
                $puser = PlatformUserService::infoByAdminuserid($platformDecrypt['userid'], $platformDecrypt['platform']->id);
            }
            if (! $puser) {
                if ($loginType == 'platform') {
                    if ($request->ajax()) {
                        return $this->fail(['url' => config('admin.admin.common.PLATFORM_LOGIN_PAGE')], '平台用户不存在');
                    } else {
                        return Redirect::to('/403')->with('data', ['msg' => '平台用户不存在', 'url' => config('admin.admin.common.PLATFORM_LOGIN_PAGE')]);
                    }
                } elseif ($loginType == 'admin') {
                    if ($request->ajax()) {
                        return $this->fail(['url' => config('admin.admin.common.ADMIN_LOGIN_PAGE')], $seeting['admin']['login']['business_description']);
                    } else {
                        return Redirect::to('/403')->with('data', ['msg' => '平台用户不存在', 'url' => config('admin.admin.common.ADMIN_LOGIN_PAGE')]);
                    }
                }
            }
        } else {
            return $request->expectsJson() ? @null : redirect(config('/405'));
        }

        // 记录操作日志
        if ($request->isMethod('POST') || $request->isMethod('PUT') || $request->isMethod('DELETE')) {
            // 记录操作
            //            AdminLog::recordPlatform(2, '', $request->input(), $login_info, $puser);
        }

        //合并参数
        $request->merge(['setting' => $seeting]);
        $request->merge(['user' => $loginInfo]);
        $request->merge(['platform_user' => $puser]);
        $request->merge(['platform' => $platformDecrypt['platform']]);
        $request->merge(['platform_number' => $platformDecrypt['number']]);
        $request->merge(['loginType' => $loginType]);

        return $next($request);
    }
}
